package com.example.hrmsaas.system.controller;

import com.example.hrmsaas.system.service.IPermissionService;
import com.example.hrmsaas.system.utils.DataGridView;
import com.example.hrmsaas.system.utils.ResultObject;
import com.example.hrmsaas.system.utils.vo.PermissionVo;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 雷哥
 * @since 2021-11-10
 */
@RestController
@RequestMapping("/system/sys-permission")
public class PermissionController extends BaseController {
    private Log log = (Log) LogFactory.getLog(PermissionController.class);

    @Autowired
    IPermissionService permissionService;


    @RequestMapping("/permissionList")
    public DataGridView getPermissionList(){
         try {
             PermissionVo permVo = new PermissionVo();
            Integer page = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 1;
            Integer limit = request.getParameter("limit") != null  ? Integer.parseInt(request.getParameter("limit")) :10;
            Integer type  = request.getParameter("type") != null ? Integer.parseInt(request.getParameter("type")) :0;
            String  pid   = request.getParameter("pid") != null ? String.valueOf(request.getParameter("pid")):null;
            String enVisible = request.getParameter("enVisible");
            PermissionVo roleVoQueryMapper = this.getpermVoQueryMapper(permVo, page, limit, CompanyId,type,pid,enVisible);
            return permissionService.getPermissionList(roleVoQueryMapper);
        } catch (Exception e) {
            log.error("PermissionController列表查询异常："+e.getLocalizedMessage());
            return new DataGridView();
        }
    }

    private PermissionVo getpermVoQueryMapper(PermissionVo permVo, Integer page, Integer limit, String companyId,Integer type,String pid,String enVisible) {
        if (null != page) permVo.setPage(page);
        if (null != limit) permVo.setLimit(limit);
        if(type>0)permVo.setType(type);
        if(null != pid)permVo.setPid(pid);
        if(null != enVisible)permVo.setEnVisible(enVisible);
        permVo.setCompanyId(companyId);
        return permVo;
    }

    @RequestMapping("/permissionAll")
    public DataGridView getPermissionAll(){
        try {
            PermissionVo permVo = new PermissionVo();
            Integer type  = Integer.parseInt(request.getParameter("type"));
            permVo.setType(type);
            return new DataGridView(permissionService.getPermissionAll(permVo));
        } catch (Exception e) {
            log.error("PermissionController列表查询异常："+e.getLocalizedMessage());
            return new DataGridView();
        }
    }



    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultObject add(@RequestBody Map<String,Object> map) throws Exception{
        try {

            if (permissionService.add(map)) {
                return ResultObject.ADD_SUCCESS;
            } else {
                return ResultObject.ADD_ERROR;
            }
        } catch (Exception e) {
            log.error("新增权限异常" + e.getMessage());
            return ResultObject.ADD_ERROR;
        }
    }


    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ResultObject edit(@RequestBody Map<String,Object> map)throws Exception{
        try {

            if (permissionService.edit(map)) {
                return ResultObject.SAVE_SUCCESS;
            } else {
                return ResultObject.SAVE_ERROR;
            }
        } catch (Exception e) {
            log.error("更新权限异常" + e.getMessage());
            return ResultObject.ADD_ERROR;
        }

    }

    @RequestMapping("/info")
    public DataGridView detail() throws Exception {
        String id = request.getParameter("id");
        return permissionService.info(id);
    }

    @RequestMapping("/delet")
    public ResultObject del() throws Exception {
        String id = request.getParameter("id");
        boolean code=permissionService.del(id);
        if(code){
            return ResultObject.DELETE_SUCCESS;
        }else{
            return ResultObject.DELETE_ERROR;
        }
    }

}

